<template>
  <div class="common-container">
    <div class="header">
      <div class="header_left">
        <el-input
          v-model="searchForm.productModel"
          clearable
          @clear="onClear"
          placeholder="请输入产品型号"
          size="small"
          style="width: 220px"
          maxlength="20"
        >
          <el-button
            slot="append"
            :icon="loadingSearch ? 'el-icon-loading' : 'el-icon-search'"
            @click="searchClick"
          >
          </el-button>
        </el-input>
        <el-select
          v-model="searchForm.protocol"
          placeholder="通信协议"
          size="small"
          style="margin-left: 15px"
          clearable
          @change="searchClick"
        >
          <el-option
            v-for="item in protocolList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </div>

      <div class="header_right">
        <el-button
          size="small"
          icon="el-icon-plus"
          style="margin-left: 15px"
          @click="addClick"
          v-permission="{ action: 'command:add' }"
          >新增</el-button
        >
      </div>
    </div>
    <div class="content">
      <el-table
        :data="dataList"
        height="calc(100% - 65px)"
        class="table"
        ref="refTable"
        row-key="id"
        :header-cell-style="headerStyle"
      >
        <el-table-column align="center" label="序号" type="index" width="80">
        </el-table-column>

        <el-table-column align="center" prop="productModel" label="产品型号">
        </el-table-column>
        <el-table-column
          align="center"
          prop="protocol"
          label="通信协议"
          show-overflow-tooltip
        >
          <template slot-scope="{ row }">
            <span v-if="row.protocol == 1">电信物模型标准规范</span>
            <span v-else-if="row.protocol == 2">移动铠湾规范</span>
            <span v-else-if="row.protocol == 3">移动安消规范</span>
            <span v-else-if="row.protocol == 4">萤石协议</span>
            <span v-else-if="row.protocol == 5">国标协议</span>
            <span v-else-if="row.protocol == 6">声光规范</span>
            <span v-else-if="row.protocol == 7">珠海慧芯</span>
            <span v-else>-</span>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="serviceType" label="服务标识">
        </el-table-column>
        <el-table-column align="center" prop="serviceName" label="服务名称">
        </el-table-column>
        <el-table-column align="center" prop="createTime" label="创建时间">
        </el-table-column>

        <el-table-column align="center" label="操作" width="150">
          <template slot-scope="scope">
            <el-tooltip
              class="item"
              effect="dark"
              content="编辑"
              placement="bottom"
              v-permission="{ action: 'command:edit' }"
            >
              <i
                @click="editClick(scope.row)"
                class="iconfont tianxie tableBtnIcon"
              ></i>
            </el-tooltip>
            <el-tooltip
              class="item"
              effect="dark"
              content="删除"
              placement="bottom"
              v-permission="{ action: 'command:delete' }"
            >
              <i
                @click="delClick(scope.row)"
                class="iconfont shanchu tableBtnIcon"
              ></i>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        :page.sync="page"
        :limit.sync="limit"
        :total="total"
        @pagination="getCommandServicePage"
        class="pagination"
      >
      </pagination>
    </div>
    <addCommandService ref="refAdd"></addCommandService>
    <editCommandService ref="refEdit" :editData="editData"></editCommandService>
  </div>
</template>

<script>
import { reqCommandServicePage, reqCommandServiceRemove } from "@api/api.js";
import tableStyleMixin from "@mixin/table-style";
import Pagination from "@components/Pagination.vue";
import { mapState } from "vuex";
import addCommandService from "./components/addCommandService.vue";
import editCommandService from "./components/editCommandService.vue";
import loadMore from "@mixin/loadMore";

export default {
  name: "command",
  components: {
    Pagination,
    addCommandService,
    editCommandService,
  },
  mixins: [tableStyleMixin, loadMore],
  data() {
    return {
      searchForm: {
        productId: "",
        productModel: "",
        protocol: "",
      },
      loadingSearch: false,
      productList: [],
      protocolList: [
        {
          id: 1,
          name: "电信物模型标准规范",
        },
        {
          id: 2,
          name: "移动铠湾规范",
        },
        {
          id: 3,
          name: "移动安消规范",
        },
        {
          id: 4,
          name: "萤石协议",
        },
        {
          id: 5,
          name: "国标协议",
        },
        {
          id: 6,
          name: "声光规范",
        },
        {
          id: 7,
          name: "珠海慧芯",
        },
      ],
      productModelList: [],
      dataList: [],
      page: 1,
      limit: 10,
      total: 0,
      editData: {}, //编辑数据
    };
  },
  computed: {
    ...mapState(["user"]),
  },
  mounted() {
    this.getCommandServicePage();
    // this.ProductModelPage()
  },
  methods: {
    //   获取产品列表
    // async getProductList() {
    //     const res = await reqProductList();
    //     if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
    //     this.productList = res.data.rows;
    // },
    // onChange(e) {
    //     if (e == '') {
    //         this.searchForm.productModel = ''
    //         this.productModelList = []
    //         this.clearStateList()
    //         return
    //     }
    //     this.ProductModelPage()
    // },
    onClear() {
      this.clearStateList();
    },
    // 获取产品型号
    // async ProductModelPage() {
    //     let res = await reqProductModelPage(1, 50, {
    //         productId: '',
    //     });
    //     // console.log(res);
    //     if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
    //     this.restaurants =  res.data.rows
    // },

    clearStateList() {
      this.page = 1;
      this.limit = 10;
      this.getCommandServicePage();
    },
    // 列表
    async getCommandServicePage() {
      const res = await reqCommandServicePage(this.page, this.limit, {
        productModel: this.searchForm.productModel,
        protocol: this.searchForm.protocol,
      });
      if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
      this.dataList = res.data.rows;
      this.total = res.data.total;
    },
    // 搜索
    async searchClick() {
      this.page = 1;
      this.limit = 10;
      this.loadingSearch = true;
      await this.getCommandServicePage();
      this.loadingSearch = false;
    },
    // 新增
    addClick() {
      this.$refs.refAdd.dialogVisible = true;
      this.$refs.refAdd.getProductList();
    },
    // 编辑
    editClick(row) {
      row.inputType = row.inputType + "";
      if (row.inputType == 1) {
        row.dataDefinition1 = row.dataDefinition;
        row.dataDefinition2 = [
          {
            key: "",
            value: "",
          },
        ];
      } else {
        row.dataDefinition1 = "";
        row.dataDefinition2 = row.dataDefinition;
      }
      this.editData = JSON.parse(JSON.stringify(row));
      this.$refs.refEdit.dialogVisible = true;
    },
    // 删除
    async delClick(row) {
      const result = await this.$confirm(
        `此操作将删除该指令, 是否继续?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch(() => {});
      if (result !== "confirm") return;
      const res = await reqCommandServiceRemove(row.id);
      if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
      this.$modal.notifySuccess("删除指令成功");
      this.getCommandServicePage();
    },
  },
};
</script>

<style scoped lang="less">
@import url("~@views/style/tablePage.less");

.common-container {
  box-shadow: none !important;

  // background-color: transparent;
  .header {
    background-color: @primaryHeaderBgcolor;
    border-radius: 6px 6px 0 0;
    position: relative;
  }
}

.popover_from {
  ::v-deep .el-input__inner {
    background-color: #fff !important;
  }
}

.popover_footer {
  width: 100%;
  display: flex;
  justify-content: center;
}

.content {
  height: calc(100% - 61px) !important;
}

::v-deep .el-button--small {
  background: #7367f0;
  color: #fff;
  border: none;
}

// 下拉框加载开始
.div_box {
  height: 276px;
  position: absolute;
  top: -1px;
  left: -1px;
  width: 190px;
  z-index: 9999;
}

::v-deep .el-loading-mask {
  border-radius: 4px;
}

// 下拉框加载结束
</style>